package com.sis.main.server.jdo;

import java.util.List;

import javax.jdo.PersistenceManager;

import com.sis.main.client.DataSecurity;

public class GoogleDatabaseService {
	
	public PersistenceManager getPersistenceManager() {
		return PersistenceFactory.get().getPersistenceManager();
	}
	
	  public List<DataSecurity> selectWatchList() {
		   	PersistenceManager persistencemanager = getPersistenceManager();
	    	String query = "select from " + DataSecurity.class.getName();
	    	List<DataSecurity> dataSecurities =  
	    		(List<DataSecurity>) persistencemanager.newQuery(query).execute();
	    	List<DataSecurity> detachedSecurities = 
	    			(List<DataSecurity>) persistencemanager.detachCopyAll(dataSecurities);
	    	persistencemanager.close();
	    	return detachedSecurities;
	  }
	
	  public DataSecurity saveSecurity(DataSecurity dataSecurity){
		  PersistenceManager persistencemanager = getPersistenceManager();
		  try {
//			  DataSecurity dataSecurityToSave = 
//				  (DataSecurity) persistencemanager.getObjectById(DataSecurity.class,
//						  dataSecurity.getID());
//			  dataSecurityToSave.setLast(dataSecurity.getLast());
//			  dataSecurityToSave.setChange(dataSecurity.getChange());
//			  dataSecurityToSave.setPercentChange(dataSecurity.getPercentChange());
			  getPersistenceManager().makePersistent(dataSecurity);
		  }catch (Exception e) {
			  e.printStackTrace();
	      } finally {
	    	  persistencemanager.close();
	      }
	    return dataSecurity;
	  }
	  
	  public boolean deleteSecurity(DataSecurity dataSecurity) {
		  PersistenceManager persistencemanager = getPersistenceManager();
		  try {
			  // Retrieves the object to delete by its id; note, the dataSecurity 
			  //argument variable coming in is transient 
			  //and therefore can't be used directly to delete object from the db
			  DataSecurity dataSecurityToDelete = 
				  (DataSecurity) persistencemanager.getObjectById(DataSecurity.class,
						  dataSecurity.getID());  
			  persistencemanager.deletePersistent(dataSecurityToDelete);
		  }catch (Exception e) {
			  e.printStackTrace();
			  return false;
	      } finally {
	    	  persistencemanager.close();
	      }
	    return true;	  
	  }

}
